//
//  ThreadLiveViewController.m
//  TEXT
//
//  Created by liubo on 2023/10/17.
//  Copyright © 2023 刘博. All rights reserved.
//

#import "ThreadLiveViewController.h"

@interface ThreadLiveViewController ()

@property (strong, nonatomic) NSThread *thread;

@end

@implementation ThreadLiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.thread = [[NSThread alloc]initWithTarget:self selector:@selector(myRun) object:nil];
    [self.thread start];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 100, 100);
    button.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view.
}

- (void)buttonClick
{
    [self performSelector:@selector(action) onThread:self.thread withObject:nil waitUntilDone:NO ];
}

- (void)myRun {
    //只要往RunLoop中添加了timer、source或者observer就会继续执行，一个Run Loop通常必须包含一个输入源或者定时器来监听事件，如果一个都没有，Run Loop启动后立即退出。
    @autoreleasepool {
        //1、添加一个input source
//        [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
//        [[NSRunLoop currentRunLoop] run];
        //2、添加一个定时器
        NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(test) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
    }
}

- (void)test
{
    
}

- (void)action
{
    NSLog(@"哈哈哈执行了action");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
